<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Package;
use App\Application\Admin\Model\Room;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminUserService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Viporder;
use Hyperf\DbConnection\Db;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/viporder")]
class ViporderController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        //门店ID
        $store_id = $this->request->input('store_id', '');
        if ($store_id != '') {
            $where[] = ['store_id', '=', $store_id];
        }
        //订单ID
        $order_on = $this->request->input('order_on', '');
        if ($order_on != '') {
            $where[] = ['order_on', '=', $order_on];
        }
        //用户ID
        $user_id = $this->request->input('user_id', '');
        if ($user_id != '') {
            $where[] = ['user_id', '=', $user_id];
        }
        //开始时间
        $add_time = $this->request->input('add_time', '');
        if ($add_time != '') {
            $where[] = ['created_at', '>', $add_time];
        }
        //结束时间
        $end_time = $this->request->input('end_time', '');
        if ($end_time != '') {
            $where[] = ['created_at', '<', $end_time];
        }
        $isadmin = 0;
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['admin_user_id'] !== 1) {
            $store = Store::where('admin_id', $this->getadminid())->select('store_id')
                ->first() ?: [];
            $where[] = ['store_id', '=', $store['store_id']];
            $store_list = Store::where('admin_id', '=', $this->getadminid())
                ->orderBy('created_at', 'DESC')
                ->pluck('store_name', 'store_id');
        } else {
            $isadmin = 1;
            $store_list = Store::where([])
                ->orderBy('created_at', 'DESC')
                ->pluck('store_name', 'store_id');
        }

        $lists = Viporder::where($where)
            ->orderBy('order_id', 'DESC')
            ->paginate()->toArray();


        $storelist = Store::where('store_id', '>', 0)->pluck('store_name', 'store_id');
        $roomlist = Room::where('room_id', '>', 0)->pluck('name', 'room_id');
        $packagelist = Package::where('package_id', '>', 0)->pluck('name', 'package_id');
        foreach ($lists['data'] as $k => $v) {
            //店铺名称
            $lists['data'][$k]['store_name'] = $storelist[$v['store_id']];
            //包厢名称
            $lists['data'][$k]['room_name'] = $roomlist[$v['room_id']];
            //套餐名称
            $lists['data'][$k]['package_name'] = $packagelist[$v['package_id']];
        }
        return compact('lists', 'store_list', 'isadmin');
    }

    #[Api]
    #[GetMapping("index/statistics")]
    function statistics()
    {
        $where = [];
        $wherestore_list = [];
        if ($this->getadminid() !== 1) {
            $wherestore_list[] = ['admin_id', '=', $this->getadminid()];
        }
        //门店ID
        $store_id = $this->request->input('store_id', '');
        if ($store_id != '') {
            $where[] = ['store_id', '=', $store_id];
        } else {
            $store_id = Store::where('admin_id', $this->getadminid())->select('store_id')->first() ?: [];;
            $where[] = ['store_id', '=', $store_id['store_id']];

        }
        $lists = [];
        $datas = [];
        $wherec = [];
        for ($i = 0; $i < 30; $i++) {
            $j = $i - 1;
            $datas[$i] = date('Y-m-d', strtotime("-" . $i . "day"));
            $wherec[$i][] = ['created_at', '>=', date('Y-m-d', strtotime("-" . $i . "day")) . ' 00:00:00'];
            $wherec[$i][] = ['created_at', '<=', date('Y-m-d', strtotime("-" . $j . "day")) . ' 00:00:00'];
        }
        $datas[30] = '本月';
        $wherec[30][] = ['created_at', '>=', date('Y-m') . '-01 00:00:00'];
        $wherec[30][] = ['created_at', '<=', date('Y-m-d') . ' 00:00:00'];
        $datas[31] = '上月';
        $wherec[31][] = ['created_at', '>=', date('Y-m', strtotime("-1 month")) . '-01 00:00:00'];
        $wherec[31][] = ['created_at', '<=', date('Y-m') . '-01 00:00:00'];
        $datas[32] = '上上月';
        $wherec[32][] = ['created_at', '>=', date('Y-m', strtotime("-2 month")) . '-01 00:00:00'];
        $wherec[32][] = ['created_at', '>=', date('Y-m', strtotime("-1 month")) . '-01 00:00:00'];
        $datas[33] = '总计';
        $wherec[33]= [];
        foreach ($datas as $k => $v) {
            $lists['data'][$k]['date'] = $v;
            //订单营业额
            $lists['data'][$k]['order_turnover'] = Order::where($where)->where($wherec[$k])->whereIn('status', [0, 1, 2, 5])->sum('pay_price');
            //会员充值
            $lists['data'][$k]['vip_turnover'] = Order::where($where)->where($wherec[$k])->whereIn('status', [0, 1, 2, 5])->sum('pay_price');
            //优惠卷
            $lists['data'][$k]['coupon_turnover'] = Order::where($where)->where($wherec[$k])->whereIn('status', [0, 1, 2, 5])->sum('pay_price');
        }
        $store_list = Store::where($wherestore_list)
            ->orderBy('created_at', 'DESC')
            ->pluck('store_name', 'store_id');
        return compact('lists', 'store_list', 'store_id');
    }


    #[Api]
    #[GetMapping("cancelorder")]
    function cancelorder(int $order_id = 0)
    {
        $order_id = $this->request->input('order_id', '');
        $order = Order::updateOrCreate(['order_id' => $order_id], [
            'status' => 4,
            'updated_at' => time()
        ]);
        return $order ? $this->returnSuccessJson(compact('order')) : $this->returnErrorJson();
    }

    #[Api]
    #[GetMapping("refundorder")]
    function refundorder()
    {
        $order_id = $this->request->input('order_id', '');
        Db::beginTransaction();
        try {
            //退款开始

            //退款结束
            //退款成功修改订单状态
            $order = Order::updateOrCreate(['order_id' => $order_id], [
                'status' => 3,
                'updated_at' => time()
            ]);
            Db::commit();
        } catch (\Throwable $ex) {
            Db::rollBack();
        }
        return $order ? $this->returnSuccessJson(compact('order')) : $this->returnErrorJson($ex->getMessage());
    }
}
